package com.it.controller;


import com.it.entity.dispatch.AiClaimDispatch;
import com.it.service.AiClaimDispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.R;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 查勘调度表（报案受理后生成的查勘任务调度信息） 前端控制器
 * </p>
 *
 * @author lutao
 * @since 2025年09月19日
 */
@RestController
@RequestMapping("/aiClaimDispatch")
public class AiClaimDispatchController {

    @Autowired
    private AiClaimDispatchService aiClaimDispatchService;

    /**
     * 查勘调度表（查勘任务调度信息）列表查询
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1",value = "pageNum",required = false) Integer pageNum,
                  @RequestParam(defaultValue = "5",value = "pageSize",required = false) Integer pageSize,
                  @RequestParam(value = "reportId",required = false) String reportId,
                  @RequestParam(value = "surveyWorkNo",required = false) String surveyWorkNo,
                  @RequestParam(value = "dispatchStatus",required = false) Integer dispatchStatus) {
        return aiClaimDispatchService.list(pageNum,pageSize,reportId,surveyWorkNo,dispatchStatus);
    }
    /**
     * 更新查勘调度表（查勘任务调度信息）
     */
    @PutMapping("/{dispatchId}")
    public R update(@RequestBody AiClaimDispatch aiClaimDispatch, @PathVariable("dispatchId") Integer dispatchId) {
        aiClaimDispatch.setDispatchId(dispatchId);
        return R.Success("更新调度表成功",aiClaimDispatchService.updateById(aiClaimDispatch));
    }
    /**
     * 删除调度记录
     */
    @DeleteMapping("/{dispatchId}")
    public R delete(@PathVariable("dispatchId") Integer dispatchId) {
       return R.Success("删除调度记录成功",aiClaimDispatchService.removeById(dispatchId));
    }
    /**
     * 导出数据
     * @return
     */
    @GetMapping("/export")
    public void export(@RequestParam(defaultValue = "1",value = "pageNum",required = false) Integer pageNum,
                  @RequestParam(defaultValue = "5",value = "pageSize",required = false) Integer pageSize,
                  @RequestParam(value = "reportId",required = false) String reportId,
                  @RequestParam(value = "surveyWorkNo",required = false) String surveyWorkNo,
                  @RequestParam(value = "dispatchStatus",required = false) Integer dispatchStatus,
                  HttpServletResponse response) {
        aiClaimDispatchService.export(pageNum,pageSize,reportId,surveyWorkNo,dispatchStatus,response);
    }

    /**
     * 查询报案未调度的任务
     */
    @GetMapping("/undispatchList")
    public R undispatchList(@RequestParam(defaultValue = "1",value = "pageNum",required = false) Integer pageNum,
                            @RequestParam(defaultValue = "10",value = "pageSize",required = false) Integer pageSize) {
        return aiClaimDispatchService.undispatchList(pageNum,pageSize);
    }
    /**
     * 新增查勘调度表（查勘任务调度信息）
     */
    @PostMapping
    public R add(@RequestBody AiClaimDispatch aiClaimDispatch) {
//        aiClaimDispatch.setDispatchTime(LocalDateTime.now());
        aiClaimDispatchService.addDispatch(aiClaimDispatch);
        return R.Success("新增查勘调度表成功");
    }

    // 修改调度表状态
    @PutMapping("/updateStatus")
    public R updateStatus(@RequestParam("reportNo") String reportNo,
                          @RequestParam("surveyWorkNo") String surveyWorkNo,
                          @RequestParam("dispatchStatus") Integer dispatchStatus){
        return aiClaimDispatchService.update(reportNo,surveyWorkNo,dispatchStatus);
    }


    //根据报案号查询所有调度记录
    @GetMapping("/selectByReportNo")
    public List<AiClaimDispatch> selectByReportNo(@RequestParam("reportNo") String reportNo){
        return aiClaimDispatchService.selectByReportNo(reportNo);
    }
}
